package com.cxy.datawarehouse.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {

    private ApiKey apiKey() {
        return new ApiKey("token", "token", "header");
    }

    @Bean(value = "dataCleanDocket")
    public Docket dataCleanDocket() {
        Docket docket = docket();
        //分组名称
        docket.groupName("数据清洗后端接口");
        ApiSelectorBuilder select = docket.select();
        select.apis(RequestHandlerSelectors.basePackage("com.cxy.datawarehouse.controller"));
        select.paths(PathSelectors.any());
        select.build();
        docket.securitySchemes(Collections.singletonList(apiKey()));
        return docket;
    }

    private Docket docket() {
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder()
                .name("token")
                .description("认证token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.globalOperationParameters(parameters);
        docket.apiInfo(apiInfo());
        return docket;
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("cxy", "https://www.baidu.com", "1527707655@qq.com");
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title("数据仓库");
        apiInfoBuilder.description("数据仓库");
        apiInfoBuilder.termsOfServiceUrl("https://www.baidu.com");
        apiInfoBuilder.contact(contact);
        apiInfoBuilder.version("1.0.0");
        return apiInfoBuilder.build();
    }
}
